package com.situ.mvc.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mvc.dao.impl.StudentDaoImpl;
import com.situ.mvc.entity.PageBean;
import com.situ.mvc.entity.Student;
import com.situ.mvc.mapper.IStudentMapper;
import com.situ.mvc.mapper.IUserMapper;
import com.situ.mvc.service.IStudentService;
import com.situ.mvc.utill.MyBatisUtil;
@Service
public class StudentServiceImpl implements IStudentService{
	@Autowired
	private IStudentMapper studentMapper;

	@Override
	public List<Student> selectAll() {
	//	SqlSession sqlSession = MyBatisUtil.getSqlSession();
		//studentMapper = sqlSession.getMapper(IStudentMapper.class);

		// 体现Service业务逻辑，给所有学生名字前面加上"Java1907"
		List<Student> list = studentMapper.selectAll();
		for (Student student : list) {
			String name = student.getName();
			student.setName("Java1907-" + name);
		}
		
		return list;
	}

	@Override
	public List<Student> search(String name) {
		return studentMapper.search(name);
	}

	@Override
	public boolean insert(Student student) {
		// 1、数据库中查看是不是有该姓名的学生 selectByName(name)
		// 2.1、如果有：表示该名字用户已经存在,不去调用insert
		// 2.2、如果没有：表示该名字用户不存在,去调用insert插入这个用户
		// select count(*) from student where name='zhansgan';
	//	SqlSession sqlSession = MyBatisUtil.getSqlSession();
	//	studentMapper = sqlSession.getMapper(IStudentMapper.class);
		List<Student> list =  studentMapper.selectByName(student.getName());
	
		if (list.size() > 0) {
			System.out.println("该用户已经存在");
			// 该名字用户已经存在
			return false;
		} else {
			studentMapper.insert(student);
		//	sqlSession.commit();
			//sqlSession.close();
			//return count == 1 ? true : false;
			return true;
		}
	}

	@Override
	public boolean update(Student student) {
		//SqlSession sqlSession = MyBatisUtil.getSqlSession();
		//studentMapper = sqlSession.getMapper(IStudentMapper.class);
		int count = studentMapper.update(student);
	//	sqlSession.commit();
	//	sqlSession.close();
		return count == 1 ? true : false;
		//return true;
	}

	@Override
	public boolean deleteById(Integer id) {
		//SqlSession sqlSession = MyBatisUtil.getSqlSession();
	//	studentMapper = sqlSession.getMapper(IStudentMapper.class);
		// 业务逻辑：id在10-100之间的不删除
		int count = studentMapper.deleteById(id);
		//	sqlSession.commit();
		//	sqlSession.close();
			return count == 1 ? true : false;
	}

	@Override
	public PageBean pageList(Integer pageNo, Integer pageSize) {
		//SqlSession sqlSession = MyBatisUtil.getSqlSession();
		///studentMapper = sqlSession.getMapper(IStudentMapper.class);
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(pageNo);
		// 一页有多少条数据
		pageBean.setPageSize(pageSize);
		// 一共有多少页
		int count = studentMapper.getTotalCount();
		int totalPage = (int) Math.ceil((double)count / pageSize);
		pageBean.setTotalPage(totalPage);
		// 当前页数据
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentMapper.pageList(offset, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}
	
	@Override
	public List<Map<String, Object>> banjixuanke() {
		// TODO Auto-generated method stub
		return studentMapper.banjixuanke();
	}

	@Override
	public Student selectById(int id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		studentMapper = sqlSession.getMapper(IStudentMapper.class);
		return studentMapper.selectById(id);
	}

	
	public static void main(String[] args) {
		int count = 10;
		int pageSize = 3;
		// Type mismatch: cannot convert from double to int
		int totalPage = (int) Math.ceil((double)count / pageSize);
		System.out.println(totalPage);
	}

	@Override
	public boolean checkName(String name) {
		List<Student> list = studentMapper.selectByName(name);
//		if (list.size() > 0) {
//			return true;
//		} else {
//			return false;
//		}
		return list.size() > 0 ? true : false;
	}

	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		return studentMapper.selectStudentBanjiInfo();
	}
}
